<?php
if(!defined('FROM_INDEX'))exit('Invalid request');

require_once ROOT_PATH.'include/sql/class_db.php';
require_once ROOT_PATH.'include/sql/sql_queries.php';

function get_user($request)
{
	$uid = isset($request['id']) ? $request['id'] : '';
	$name = isset($request['name']) ? $request['name'] : '';
	
	$key = md5("get_user_$uid"."_$name");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$sql = get_user_sql();
	
	$results = DBFunction::getInstance()->getRow($sql, array($uid, $name));
	
	if(check_array($results))
	{
		cache_set($key, $results);
		return $results;
	}
	else
	{
		return array();
	}
}

function get_user_travel_item($uid)
{
	$sql = get_user_travel_item_sql();
	$result = DBFunction::getInstance()->getAll($sql, array($uid));
	
	return $result;
}

function add_user_travel_item($request)
{
	$sql = add_user_travel_item_sql();
	$result = DBFunction::getInstance()->insert($sql, array($request['uid'], $request['item_id'], $request['type']));
	
	return $result;
}

function delete_user_travel_item()
{
	$sql = delete_user_travel_item_sql();
	$result = DBFunction::getInstance()->insert($sql, array($request['uid'], $request['item_id']));
	
	return ($result !== false) ? true : false;
}

function get_user_group_list()
{
	$key = md5("get_user_group_list");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$sql = get_user_group_list_sql();
	$results = DBFunction::getInstance()->getAll($sql);
	
	if(check_array($results))
	{
		cache_set($key, $results);
		return $results;
	}
	else
	{
		return array();
	}
}

function add_user($request)
{
	$params = array(
		$request['name'],
		$request['password'],
		$request['phone'],
		$request['group_id'],
		$request['time'],
		$request['time']
	);
	
	$sql = add_user_sql();
	
	$result = DBFunction::getInstance()->insert($sql, $params);
	
	return ($result !== false) ? $result : false;
}

function update_user($request)
{
	$params = array(
		$request['name'],
		$request['password'],
		$request['phone'],
		$request['group_id'],
		$request['id']
	);
	
	$sql = update_user_sql();
	$result = DBFunction::getInstance()->update($sql, $params);
	
	return ($result !== false) ? $result : false;
}

function get_visitor($request)
{
	$id = isset($request['id']) ? $request['id'] : '';
	$phone = isset($request['phone']) ? $request['phone'] : '';
	
	$sql = get_visitor_sql();
	$results = DBFunction::getInstance()->getRow($sql, array($id, $phone));
	
	return $results;
}

function add_visitor($request)
{
	$params = array(
		$request['phone'],
		$request['nick_name'],
		$request['password'],
		$request['time'],
		$request['time'],
		$request['last_ip']
	);
	
	$sql = add_visitor_sql();
	
	$result = DBFunction::getInstance()->insert($sql, $params);
	
	return $result;
}

function update_visitor($request)
{
	$sql = update_visitor_sql();
	$result = DBFunction::getInstance()->update($sql, array($request['phone'], $request['nick_name'], $request['password'], $request['id']));

	return ($result !== false) ? $result : false;
}

function delete_visitor($id)
{
	$sql = delete_visitor_sql();
	$result = DBFunction::getInstance()->update($sql, array($id));
	
	return ($result !== false) ? $result : false;
}

function delete_user($id)
{
	$sql = delete_user_sql();
	$result = DBFunction::getInstance()->execute($sql,  array($id));

	return ($result !== false) ? true : false;
}

function get_business($request)
{
	$bid = isset($request['bid']) ? $request['bid'] : '';
	$name = isset($request['name']) ? $request['name'] : '';
	
	$key = md5("get_business_$bid"."_$name");
	
	$result = cache_get($key);
	if($result !== false)
	{
		return $result;
	}

	$sql = get_avg_comment_score_sql();
	$temp_result = DBFunction::getInstance()->getRow($sql, array($bid));
	
	// update custome comment score
	if($bid && check_array($temp_result))
	{
		list($service_score, $env_score, $price_score) = array_values($temp_result);
		$total = round(($service_score + $env_score + $price_score) / 3, 2);
		
		$sql = update_total_score_sql();
		DBFunction::getInstance()->update($sql, array($total, $bid));
		
		$sql = update_business_score_sql();
		DBFunction::getInstance()->update($sql, array($service_score, $env_score, $price_score, $bid));
	}
	
	$sql = get_business_sql();
	$result = DBFunction::getInstance()->getRow($sql, array($bid, $name));
	
	if(check_array($result))
	{
		cache_set($key, $result);
		return $result;
	}
	else
	{
		return array();
	}
}

function get_comment_count($request)
{
	$id = isset($request['id']) ? $request['id'] : '';
	
	$key = md5("get_comment_count_$id");
	
	$result = cache_get($key);
	if($result !== false)
	{
		return $result;
	}
	
	$sql = get_comment_count_sql();
	$result = DBFunction::getInstance()->getOne($sql, array($id));

	cache_set($key, $result);
	return $result;
}

//TODO, when name == param, show first
function get_business_list($request)
{
	$ids = isset($request['ids']) ? $request['ids'] : '';
	
	$latitude1 = isset($request['latitude1']) ? $request['latitude1'] : '';
	$latitude2 = isset($request['latitude2']) ? $request['latitude2'] : '';
	$longitude1 = isset($request['longitude1']) ? $request['longitude1'] : '';
	$longitude2 = isset($request['longitude2']) ? $request['longitude2'] : '';
	
	$name = isset($request['name']) ? $request['name'] : '';
	$legal_person = isset($request['legal_person']) ? $request['legal_person'] : '';
	$limit = isset($request['limit']) ? $request['limit'] : DEFAULT_PER_PAGE;
	$offset = isset($request['offset']) ? $request['offset'] : '0';
	$offset *= $limit;
	
	$sort_fields = array('name'=>'ti.`name`', 'price'=>'tb.price_score', 'service'=>'tb.service_score', 'env'=>'tb.env_score');
	$sort = (isset($request['sort']) && isset($sort_fields[$request['sort']])) ? $sort_fields[$request['sort']] : 'ti.`name`';
	$order = (isset($request['order']) && $request['order'] == 'desc') ? 'desc' : 'asc';
	
	$key = md5("get_business_list_$name"."_$ids"."_$legal_person"."_$latitude1"."_$longitude1"."_$latitude2"."_$longitude2"."_$sort"."_$order"."_$limit"."_$offset");
	
	$result = cache_get($key);
	if($result !== false)
	{
		return $result;
	}
	
	$params = array();
	$where = '1';
	if($longitude1 && $longitude2 && $latitude1 && $latitude2)
	{
		$params = array($longitude2, $longitude1, $latitude2, $latitude1);
		$where = 'ti.`longitude` <= ?  and ti.`longitude` >= ? and ti.`latitude` <= ? and ti.`latitude` >= ?';
	}
	else if($name)
	{
		$params = array('%'.strtolower($name).'%');
		$where = 'ti.`name` like ?';
	}
	else if($legal_person)
	{
		$params = array('%'.strtolower($legal_person).'%');
		$where = 'tb.`legal_person` like ?';
	}
	else if($ids)
	{
		$where = "ti.`id` in ($ids)";
	}
	
	$sql = get_business_list_sql();
	$sql .= $where;
	$sql .= " order by $sort $order";
	
	$result['count'] = DBFunction::getInstance()->getRowCount($sql, $params);
	$result['result'] = array();
	
	if($result['count'] > 0)
	{
		$result['result'] = DBFunction::getInstance()->selectLimit($sql, $limit, $offset, $params);
		
		cache_set($key, $result);
	}
	
	return $result;
}

function check_travel_item_exists($name, $id)
{
	$sql = check_travel_item_exists_sql();
	$result = DBFunction::getInstance()->getOne($sql, array($name, $id));
	
	return $result ? true : false;
}

function add_business($request)
{
	$date = time();
	$uid = isset($request['uid']) ? $request['uid'] : '';
	
	$id = isset($request['id']) ? $request['id'] : '';
	
	$name = isset($request['name']) ? $request['name'] : '';
	$logo = isset($request['image']) ? $request['image'] : '';
	$legal_person = isset($request['legal_person']) ? $request['legal_person'] : '';
	$number = isset($request['number']) ? $request['number'] : '';
	
	$telephone = isset($request['telephone']) ? $request['telephone'] : '';
	$avg_cost = isset($request['avg_cost']) ? $request['avg_cost'] : '';
	$desc = isset($request['desc']) ? $request['desc'] : '';
	
	$business_scope = isset($request['business_scope']) ? $request['business_scope'] : '';
	$special_count = isset($request['special_count']) ? (int)$request['special_count'] : '3';
	$address = isset($request['address']) ? $request['address'] : '';
	
	$longitude = isset($request['longitude']) ? (float)$request['longitude'] : '';
	$latitude = isset($request['latitude']) ? (float)$request['latitude'] : '';
	$is_validate = isset($request['is_validate']) ? $request['is_validate'] : '1';

	$result = false;
	if($id)
	{
		$params = array($name, $legal_person, $number, $logo, 
					$address, $telephone,
					$latitude, $longitude, $uid, $date,
					$id);
		$sql = update_travel_item_sql();
		$result = DBFunction::getInstance()->update($sql, $params);
	}
	else
	{
		$params = array($name, $legal_person, $number, $logo, 
						$address, $telephone, '0', 
						$longitude, $latitude, 
						$uid, $date, $uid, $date);
		$sql = add_travel_item_sql();
		$id = DBFunction::getInstance()->insert($sql, $params);
	}
	
	if($id)
	{
		$params = array($id, $legal_person, $number, $business_scope, 
						$avg_cost, $desc, '0', '0', 
						'0', $special_count, $is_validate);
	
		$sql = add_business_sql();
		$result = DBFunction::getInstance()->insert($sql, $params);
	}
	
	return $id;
}

function update_business($request)
{
	$date = time();
	$uid = isset($request['uid']) ? $request['uid'] : '';
	$bid = isset($request['id']) ? $request['id'] : '';
	
	$name = isset($request['name']) ? $request['name'] : '';
	$image = isset($request['image']) ? $request['image'] : '';
	$legal_person = isset($request['legal_person']) ? $request['legal_person'] : '';
	$number = isset($request['number']) ? $request['number'] : '';
	
	$telephone = isset($request['telephone']) ? $request['telephone'] : '';
	$avg_cost = isset($request['avg_cost']) ? $request['avg_cost'] : '';
	$desc = isset($request['desc']) ? $request['desc'] : '';
	
	$business_scope = isset($request['business_scope']) ? $request['business_scope'] : '';
	//$gov_score = isset($request['gov_score']) ? (float)$request['gov_score'] : '';
	//$total_score = isset($request['total_score']) ? (float)$request['total_score'] : '';
	$address = isset($request['address']) ? $request['address'] : '';
	
	$longitude = isset($request['longitude']) ? (float)$request['longitude'] : '';
	$latitude = isset($request['latitude']) ? (float)$request['latitude'] : '';
	$is_validate = isset($request['is_validate']) ? $request['is_validate'] : '';
	
	$params = array($name, $legal_person, $number, $image, 
					$address, $telephone,
					$latitude, $longitude, $uid, $date,
					$bid);
	$sql = update_travel_item_sql();
	$result = DBFunction::getInstance()->update($sql, $params);
	
	if($result !== false)
	{
		$params = array($legal_person, $number, $business_scope, $avg_cost, 
						$desc, $is_validate, $bid);
	
		$sql = update_business_sql();
		$result = DBFunction::getInstance()->update($sql, $params);
	}
	
	return ($result !== false) ? true : false;
}

function delete_business($request)
{
	$ids = isset($request['ids']) ? $request['ids'] : '';
	
	if(stripos($ids, ','))
	{
		$ids = explode(',', $ids);
		
		foreach($ids as $id)
		{
			$sql = delete_business_sql();
			$result = DBFunction::getInstance()->execute($sql, array($id));
			
			$sql = delete_travel_item_sql();
			$result = DBFunction::getInstance()->execute($sql, array($id));
		}
	}
	else
	{
		$sql = delete_business_sql();
		$result = DBFunction::getInstance()->execute($sql,  array($ids));
		
		$sql = delete_travel_item_sql();
		$result = DBFunction::getInstance()->execute($sql, array(ids));
	}
	
	return ($result !== false) ? true : false;
}

function get_business_special_list($request)
{
	$bid = isset($request['bid']) ? $request['bid'] : '';
	
	$key = md5("get_business_special_list_$bid");
	
	$result = cache_get($key);
	if($result !== false)
	{
		return $result;
	}
	
	$sql = get_business_special_list_sql();
	$result = DBFunction::getInstance()->getAll($sql, array($bid));
	
	if(check_array($result))
	{
		cache_set($key, $result);
		return $result;
	}
	else
	{
		return array();
	}
}

function get_business_special($request)
{
	$bid = isset($request['bid']) ? $request['bid'] : '';
	$id = isset($request['id']) ? $request['id'] : '';
		
	$key = md5("get_business_special_$bid"."_$id");
	
	$result = cache_get($key);
	if($result !== false)
	{
		return $result;
	}
	
	$sql = get_business_special_sql();
	$result = DBFunction::getInstance()->getRow($sql, array($id, $bid));
	
	if(check_array($result))
	{
		cache_set($key, $result);
		return $result;
	}
	else
	{
		return array();
	}
}

function add_business_special($request)
{
	$date = time();
	$uid = isset($request['uid']) ? $request['uid'] : '';
	
	$bid = isset($request['bid']) ? $request['bid'] : '';
	$name = isset($request['name']) ? $request['name'] : '';
	$price = isset($request['price']) ? (float)$request['price'] : 0;
	$star_level = isset($request['star_level']) ? (float)$request['star_level'] : 0;
	
	$image = isset($request['image']) ? $request['image'] : '';
	$desc = isset($request['desc']) ? $request['desc'] : '';
	
	$params = array($bid, $name, $price, $star_level,
					$desc, $image,$uid, $date, 
					$uid, $date);

	$sql = add_business_special_sql();
	$result = DBFunction::getInstance()->insert($sql, $params);
	
	return $result;
}

function update_business_special($request)
{
	$date = time();
	$uid = isset($request['uid']) ? $request['uid'] : '';
	$bid = isset($request['bid']) ? $request['bid'] : '';
	$id = isset($request['id']) ? $request['id'] : '';
	
	$name = isset($request['name']) ? $request['name'] : '';
	$price = isset($request['price']) ? (float)$request['price'] : 0;
	$star_level = isset($request['star_level']) ? (float)$request['star_level'] : 0;
	
	$image = isset($request['image']) ? $request['image'] : '';
	$desc = isset($request['desc']) ? $request['desc'] : '';
	
	$params = array($name, $price, $star_level,
					$desc, $image,$uid, $date, 
					$id, $bid);

	$sql = update_business_special_sql();
	$result = DBFunction::getInstance()->update($sql, $params);
	
	return ($result !== false) ? true : false;
}

function delete_business_special($request)
{
	$ids = isset($request['ids']) ? $request['ids'] : '';
	$bid = isset($request['bid']) ? $request['bid'] : '';
	
	if(stripos($ids, ','))
	{
		$ids = explode(',', $ids);
		
		foreach($ids as $id)
		{
			$params = array($id, $bid);

			$sql = delete_business_special_sql();
			$result = DBFunction::getInstance()->execute($sql, $params);
		}
	}
	else
	{
		$params = array($ids, $bid);
	
		$sql = delete_business_special_sql();
		$result = DBFunction::getInstance()->execute($sql, $params);
	}
	
	return ($result !== false) ? true : false;
}

function check_seafood_exists($name, $id)
{
	$sql = check_seafood_exists_sql();
	$result = DBFunction::getInstance()->getOne($sql, array($name, $id));
	
	return $result ? true : false;
}

function add_seafood($request)
{
	$date = time();
	$uid = isset($request['uid']) ? $request['uid'] : '';
	
	$name = isset($request['name']) ? $request['name'] : '';
	$units = isset($request['units']) ? $request['units'] : '';
	$dispath_price = isset($request['dispath_price']) ? (float)$request['dispath_price'] : '';
	$retail_price = isset($request['retail_price']) ? (float)$request['retail_price'] : '';
	
	//$range = isset($request['range']) ? (float)$request['range'] : '';
	$image = isset($request['image']) ? $request['image'] : '';
	
	$create_date = isset($request['create_date']) ? $request['create_date'] : '';

	$params = array($name, $image, $units, $dispath_price, 
					$retail_price, 
					$uid, $create_date, $uid, $date);

	$sql = add_seafood_sql();
	$result = DBFunction::getInstance()->insert($sql, $params);
	
	$request['id'] = $result;
	$request['is_gov'] = '0';
	add_seafood_history_price($request);
	
	return $result;
}

function get_seafood($request)
{
	$sid = isset($request['sid']) ? $request['sid'] : '';
	$name = isset($request['name']) ? $request['name'] : '';
	
	$key = md5("get_seafood_$sid"."_$name");
	
	$result = cache_get($key);
	if($result !== false)
	{
		return $result;
	}
	
	$sql = get_seafood_sql();
	$result = DBFunction::getInstance()->getRow($sql, array($sid, $name));
	
	if(check_array($result))
	{
		cache_set($key, $result);
		return $result;
	}
	else
	{
		return array();
	}
}

function update_seafood($request)
{
	$date = time();
	$uid = isset($request['uid']) ? $request['uid'] : '';
	$sid = isset($request['id']) ? $request['id'] : '';
	
	$name = isset($request['name']) ? $request['name'] : '';
	$units = isset($request['units']) ? $request['units'] : '';
	$dispath_price = isset($request['dispath_price']) ? (float)$request['dispath_price'] : '';
	$retail_price = isset($request['retail_price']) ? (float)$request['retail_price'] : '';
	
	//$range = isset($request['range']) ? (float)$request['range'] : '';
	$image = isset($request['image']) ? $request['image'] : '';
	$create_date = isset($request['create_date']) ? $request['create_date'] : '';
	
	$params = array($name, $image, $units, $dispath_price, 
					$retail_price, 
					$create_date, $uid, $date, $sid);

	$sql = update_seafood_sql();
	$result = DBFunction::getInstance()->update($sql, $params);
	
	$request['is_gov'] = '0';
	add_seafood_history_price($request);
	
	return ($result !== false) ? true : false;
}

function delete_seafood($id)
{
	$sql = delete_seafood_sql();
	$result = DBFunction::getInstance()->execute($sql, array($id));
	return ($result !== false) ? true : false;
}

function get_seafood_list($request)
{
	$name = isset($request['name']) ? $request['name'] : '';
	$limit = isset($request['limit']) ? $request['limit'] : DEFAULT_PER_PAGE;
	$offset = isset($request['offset']) ? $request['offset'] : '0';
	$offset *= $limit;
	
	$key = md5("get_seafood_$name"."_$limit"."_$offset");
	
	$result = cache_get($key);
	if($result !== false)
	{
		return $result;
	}
	
	$params = array();
	$where = '1';
	//if(strlen($name) >= 3)
	if($name)
	{
		$params = array('%'.strtolower($name).'%');
		$where = '`name` like ?';
	}
	
	$sql = get_seafood_list_sql();
	$sql .= $where;
	
	$result['count'] = DBFunction::getInstance()->getRowCount($sql, $params);
	$result['result'] = array();
	
	if($result['count'] > 0)
	{
		$result['result'] = DBFunction::getInstance()->selectLimit($sql, $limit, $offset, $params);
		
		cache_set($key, $result);
	}
	
	return $result;
}

function get_seafood_history_price($request)
{
	$id = isset($request['id']) ? $request['id'] : '';
	$name = isset($request['name']) ? $request['name'] : '';
	$from = isset($request['from']) ? strtotime($request['from']) : '';
	$to = isset($request['to']) ? strtotime($request['to']) : '';
	//$is_gov = isset($request['is_gov']) ? $request['is_gov'] : '0';
		
	$limit = isset($request['limit']) ? $request['limit'] : DEFAULT_PER_PAGE;
	$offset = isset($request['offset']) ? $request['offset'] : '0';
	$offset *= $limit;
	
	$key = md5("get_seafood_history_price_$id"."$name"."_$from"."_$to"."_$limit"."_$offset");
	
	$result = cache_get($key);
	if($result !== false)
	{
		return $result;
	}
	
	$params = array();
	$where = '1';
	//if(strlen($name) > 3)
	if($id)
	{
		$params[] = $id;
		$where = 's.`id` = ?';
	}
	if($name)
	{
		$params[] = '%'.strtolower($name).'%';
		$where = 's.`name` like ?';
	}
	if($from)
	{
		$params[] = $from;
		if($where == '1')
		{
			$where = 'hps.`create_date` >= ?';
		}
		else
		{
			$where .= ' and hps.`create_date` >= ?';
		}
	}
	if($to)
	{
		$params[] = $to;
		if($where == '1')
		{
			$where = 'hps.`create_date` <= ?';
		}
		else
		{
			$where .= ' and hps.`create_date` <= ?';
		}
	}
	
	$sql = get_seafood_history_price_sql();
	$sql .= $where;
	
	//$params[] = $is_gov;
	//$sql .= ' and `is_gov`=?';
	$sql .= ' order by hps.`retail_price` asc';
	
	$result['count'] = DBFunction::getInstance()->getRowCount($sql, $params);
	$result['result'] = array();
	
	if($result['count'] > 0)
	{
		$result['result'] = DBFunction::getInstance()->selectLimit($sql, $limit, $offset, $params);
		
		cache_set($key, $result);
	}
	
	return $result;
}

function get_new_date_seafood_history_price()
{
	$sql = get_new_date_seafood_history_price_sql();
	$result = DBFunction::getInstance()->getOne($sql);
	
	return $result;
}

function get_new_date_scenic_history_price($is_gov='1')
{
	$sql = get_new_date_scenic_history_price_sql();
	$result = DBFunction::getInstance()->getOne($sql, array($is_gov));
	
	return $result;
}

function get_new_date_hostel_history_price()
{
	$sql = get_new_date_hostel_history_price_sql();
	$result = DBFunction::getInstance()->getOne($sql);
	
	return $result;
}

function add_seafood_history_price($request)
{
	$params = array($request['id'], $request['dispath_price'], $request['retail_price'], 
					$request['is_gov'], $request['create_date']);
					
	$sql = add_seafood_history_price_sql();
	$result = DBFunction::getInstance()->insert($sql, $params);
	
	return $result;
}

function delete_seafood_history_price($id)
{
	$sql = delete_seafood_history_price_sql();
	$result = DBFunction::getInstance()->execute($sql, array($id));
	
	return ($result !== false) ? true : false;
}

function get_seafood_recommend_list($request)
{
	$sid = isset($request['sid']) ? $request['sid'] : '';
		
	$key = md5("get_seafood_recommend_list_$sid");
	
	$result = cache_get($key);
	if($result !== false)
	{
		return $result;
	}
	
	$sql = get_seafood_recommend_list_sql();
	$result = DBFunction::getInstance()->getAll($sql, array($sid));
	
	if(check_array($result))
	{
		cache_set($key, $result);
		return $result;
	}
	else
	{
		return array();
	}
}

function update_seafood_recommed_order($request)
{
	$id = isset($request['id']) ? $request['id'] : '';
	$order = isset($request['order']) ? $request['order'] : '';
	
	$params = array($order, $id);

	$sql = update_seafood_recommed_order_sql();
	$result = DBFunction::getInstance()->execute($sql, $params);
	
	return ($result !== false) ? true : false;
}

function delete_seafood_recommend($request)
{
	$sid = isset($request['sid']) ? $request['sid'] : '';
	$bid = isset($request['bid']) ? $request['bid'] : '';
	
	$params = array($sid, $bid);

	$sql = delete_seafood_recommend_sql();
	$result = DBFunction::getInstance()->execute($sql, $params);
	
	return ($result !== false) ? true : false;
}

function add_seafood_recommend($request)
{
	$date = time();
	$uid = isset($request['uid']) ? $request['uid'] : '';
	$sid = isset($request['sid']) ? $request['sid'] : '';
	$bid = isset($request['bid']) ? $request['bid'] : '';
	
	$params = array($sid, $bid, '0', '0',
					$uid, $date, $uid, $date);

	$sql = add_seafood_recommend_sql();
	$result = DBFunction::getInstance()->insert($sql, $params);
	
	return $result;
}

/*
function get_business_comment_list($request)
{
	$bid = isset($request['bid']) ? $request['bid'] : '';
	$bname = isset($request['bname']) ? $request['bname'] : '';
	$content = isset($request['content']) ? $request['content'] : '';
	
	$limit = isset($request['limit']) ? $request['limit'] : DEFAULT_PER_PAGE;
	$offset = isset($request['offset']) ? $request['offset'] : '0';
	$offset *= $limit;
	
	$key = md5("get_comment_list_$bid"."_$limit"."_$offset");
	
	$result = cache_get($key);
	if($result !== false)
	{
		return $result;
	}
	
	$sql = get_business_comment_list_sql();
	$result = DBFunction::getInstance()->selectLimit($sql, $limit, $offset, array($bid));
	
	if(check_array($result))
	{
		cache_set($key, $result);
	}
	
	return check_array($result) ? $result : array();
}
*/

function search_comment_list($request)
{
	$bid = isset($request['bid']) ? $request['bid'] : '';
	$bname = isset($request['bname']) ? $request['bname'] : '';
	$content = isset($request['content']) ? $request['content'] : '';
	
	$limit = isset($request['limit']) ? $request['limit'] : DEFAULT_PER_PAGE;
	$offset = isset($request['offset']) ? $request['offset'] : '0';
	$offset *= $limit;
	
	$key = md5("search_comment_list_$bid"."_$bname"."_$content"."_$limit"."_$offset");
	
	$result = cache_get($key);
	if($result !== false)
	{
		return $result;
	}
	
	$params = array();
	$sql = search_comment_sql();
	
	$where = '';
	if($bid)
	{
		$params[] = $bid;
		$where = 'ti.`id` = ?';
	}
	else if($bname)
	{
		$params[] = $bname;
		$where = 'ti.`name` = ?';
	}
	
	if($content)
	{
		$params[] = '%'.strtolower($content).'%';
		$where .= $where ? ' and ' : '';
		$where .= ' tc.desc like ? ';
	}
	$sql .= $where ? $where : '1';
	$sql .= ' order by tc.`id` desc';
	
	$result['count'] = DBFunction::getInstance()->getRowCount($sql, $params);
	$result['result'] = array();
	
	if($result['count'] > 0)
	{
		$result['result'] = DBFunction::getInstance()->selectLimit($sql, $limit, $offset, $params);
		
		cache_set($key, $result);
	}
	
	return $result;
}

function get_business_comment($request)
{
	$id = isset($request['id']) ? $request['id'] : '';
	
	$key = md5("get_business_comment_$id");
	
	$result = cache_get($key);
	if($result !== false)
	{
		return $result;
	}
	
	$sql = search_comment_sql();
	$sql .= 'tc.`id` = ?';
	$result = DBFunction::getInstance()->getRow($sql, array($id));
	
	if(check_array($result))
	{
		cache_set($key, $result);
		return $result;
	}
	else
	{
		return array();
	}
}

function update_business_comment($request)
{
	$date = time();
	$id = $request['id'];
	$uid = $request['uid'];
	
	$service_score = $request['service_score'];
	$env_score = $request['env_score'];
	$price_score = $request['price_score'];
	$desc = $request['desc'];
	$is_show = isset($request['is_show']) ? '1' : '0';
	
	$params = array($service_score, $env_score, $price_score, $desc, 
					$is_show, $id);

	$sql = update_business_comment_sql();
	$result = DBFunction::getInstance()->update($sql, $params);
	
	return ($result !== false) ? true : false;
}

function delete_business_comment($request)
{
	$id = isset($request['id']) ? $request['id'] : '';
	
	$sql = delete_business_comment_sql();
	$result = DBFunction::getInstance()->execute($sql, array($id));
	
	return ($result !== false) ? true : false;
}

function search_business_complaint_list($request)
{
	$bid = isset($request['bid']) ? $request['bid'] : '';
	$bname = isset($request['bname']) ? $request['bname'] : '';
	$content = isset($request['content']) ? $request['content'] : '';
	$status = $request['status'];
	
	$limit = isset($request['limit']) ? $request['limit'] : DEFAULT_PER_PAGE;
	$offset = isset($request['offset']) ? $request['offset'] : '0';
	$offset *= $limit;
	
	$key = md5("search_business_complaint_list_$bid"."_$bname"."_$content"."_$limit"."_$offset");
	
	$result = cache_get($key);
	if($result !== false)
	{
		return $result;
	}
	
	$params = array();
	$sql = search_business_complaint_sql();
	
	$where = '';
	if($bid)
	{
		$params[] = $bid;
		$where = 'ti.`id` = ?';
	}
	else if($bname)
	{
		$params[] = $bname;
		$where = 'ti.`name` = ?';
	}
	
	if($content)
	{
		$params[] = '%'.strtolower($content).'%';
		$where .= $where ? ' and ' : '';
		$where .= ' cc.reason like ? ';
	}
	
	if($status)
	{
		$params[] = $status;
		$where .= $where ? ' and ' : '';
		$where .= ' cr.`status`=? ';
	}
	
	$sql .= $where ? $where : '1';
	$sql .= ' order by cc.`id` desc';
	
	$result['count'] = DBFunction::getInstance()->getRowCount($sql, $params);
	$result['result'] = array();
	
	if($result['count'] > 0)
	{
		$result['result'] = DBFunction::getInstance()->selectLimit($sql, $limit, $offset, $params);
		
		cache_set($key, $result);
	}
	
	return $result;
}

function get_business_complaint($request)
{
	$id = isset($request['id']) ? $request['id'] : '';
	
	$key = md5("get_business_complaint_$id");
	
	$result = cache_get($key);
	if($result !== false)
	{
		return $result;
	}
	
	$sql = search_business_complaint_sql();
	$sql .= 'cc.`id` = ?';
	$result = DBFunction::getInstance()->getRow($sql, array($id));
	
	if(check_array($result))
	{
		cache_set($key, $result);
		return $result;
	}
	else
	{
		return array();
	}
}

function update_business_complaint($request)
{
	$date = time();
	$id = isset($request['id']) ? $request['id'] : '';
	$uid = isset($request['uid']) ? $request['uid'] : '';
	$reason = isset($request['reason']) ? $request['reason'] : '';
	
	$params = array($reason, $date, $uid, $id);

	$sql = update_business_complaint_sql();
	$result = DBFunction::getInstance()->update($sql, $params);
	
	return ($result !== false) ? true : false;
}

function add_complaint_result($request)
{
	$sql = add_complaint_result_sql();
	$result = DBFunction::getInstance()->insert($sql, array($request['id'], $request['status'], $request['desc']));
	
	return $result;
}

function update_complaint_result($request)
{
	$sql = update_complaint_result_sql();
	$result = DBFunction::getInstance()->insert($sql, array($request['status'], $request['desc'], $request['id']));
	
	return ($result !== false) ? true : false;
}

function delete_business_complaint($request)
{
	$id = isset($request['id']) ? $request['id'] : '';
	
	$sql = delete_business_complaint_sql();
	$result = DBFunction::getInstance()->execute($sql, array($id));
	
	return ($result !== false) ? true : false;
}

function add_customer_recommend_record()
{
	$date = time();
	$uid = isset($request['id']) ? $request['id'] : '';

	$params = array($uid, $date);

	$sql = add_customer_recommend_record_sql();
	$result = DBFunction::getInstance()->insert($sql, $params);
	
	return $result;
}

//TODO, when name == param, show first
function get_hostel_list($request)
{
	$ids = isset($request['ids']) ? $request['ids'] : '';
	$name = isset($request['name']) ? $request['name'] : '';
	
	$latitude1 = isset($request['latitude1']) ? $request['latitude1'] : '';
	$latitude2 = isset($request['latitude2']) ? $request['latitude2'] : '';
	$longitude1 = isset($request['longitude1']) ? $request['longitude1'] : '';
	$longitude2 = isset($request['longitude2']) ? $request['longitude2'] : '';
	
	$sort_fields = array('name'=>'ti.`name`', 'price'=>'th.price_score', 'service'=>'th.service_score', 'env'=>'th.env_score');
	
	$sort = (isset($request['sort']) && isset($sort_fields[$request['sort']])) ? $sort_fields[$request['sort']] : 'ti.`name`';
	$order = (isset($request['order']) && $request['order'] == 'desc') ? 'desc' : 'asc';
	
	$limit = isset($request['limit']) ? $request['limit'] : DEFAULT_PER_PAGE;
	$offset = isset($request['offset']) ? $request['offset'] : '0';
	$offset *= $limit;
	
	$key = md5("get_hostel_list_$name"."_$ids"."_$latitude1"."_$longitude1"."_$latitude2"."_$longitude2"."_$sort"."_$order"."_$limit"."_$offset");
	
	$result = cache_get($key);
	if($result !== false)
	{
		return $result;
	}
	
	$params = array();
	$where = '1';
	if($longitude1 && $longitude2 && $latitude1 && $latitude2)
	{
		$params = array($longitude2, $longitude1, $latitude2, $latitude1);
		$where = 'ti.`longitude` <= ?  and ti.`longitude` >= ? and ti.`latitude` <= ? and ti.`latitude` >= ?';
	}
	else if($name)
	{
		$params = array('%'.strtolower($name).'%');
		$where = 'ti.`name` like ?';
	}
	else if($ids)
	{
		$where = "ti.`id` in ($ids)";
	}
	
	$sql = get_hostel_list_sql();
	$sql .= $where;
	$sql .= " order by $sort $order";
	
	$result['count'] = DBFunction::getInstance()->getRowCount($sql, $params);
	$result['result'] = array();
	
	if($result['count'] > 0)
	{
		$result['result'] = DBFunction::getInstance()->selectLimit($sql, $limit, $offset, $params);
		
		cache_set($key, $result);
	}
	
	return $result;
}

function get_hostel_info($request)
{
	$id = isset($request['hid']) ? $request['hid'] : '';
	$name = isset($request['name']) ? $request['name'] : '';
	
	$key = md5("get_hostel_info_$id"."_$name");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$sql = get_avg_comment_score_sql();
	$temp_result = DBFunction::getInstance()->getRow($sql, array($id));
	
	// update custome comment score
	if($id && check_array($temp_result))
	{
		list($service_score, $env_score, $price_score) = array_values($temp_result);
		$total = round(($service_score + $env_score + $price_score) / 3, 2);
		
		$sql = update_total_score_sql();
		DBFunction::getInstance()->update($sql, array($total, $id));
		
		$sql = update_hostel_score_sql();
		DBFunction::getInstance()->update($sql, array($service_score, $env_score, $price_score, $id));
	} 
	
	$sql = get_hostel_info_sql();
	$results = DBFunction::getInstance()->getRow($sql, array($id, $name));
	
	if(check_array($results))
	{
		$id = $results['id'];
		
		$sql = get_hostel_room_list_sql();
		$results['room_list'] = DBFunction::getInstance()->getAll($sql, array($id));
		
		$sql = get_image_list_sql();
		$results['image_list'] = DBFunction::getInstance()->getAll($sql, array($id));
		
		$sql = get_comment_count_sql();
		$results['comment_count'] = DBFunction::getInstance()->getOne($sql, array($id));
		
		cache_set($key, $results);
		return $results;
	}
	else
	{
		return array();
	}
}

function add_hostel($request)
{
	$date = time();

	$uid = isset($request['uid']) ? $request['uid'] : '';
	
	$id = isset($request['id']) ? $request['id'] : '';
	
	$name = isset($request['name']) ? $request['name'] : '';
	$legal_person = isset($request['legal_person']) ? $request['legal_person'] : '';
	$number = isset($request['number']) ? $request['number'] : '';
	$image = isset($request['image']) ? $request['image'] : '';
	
	$telephone = isset($request['telephone']) ? $request['telephone'] : '';
	$address = isset($request['address']) ? $request['address'] : '';
	
	$longitude = isset($request['longitude']) ? (float)$request['longitude'] : '';
	$latitude = isset($request['latitude']) ? (float)$request['latitude'] : '';

	$result = false;
	if($id)
	{
		$params = array($request['name'], $request['legal_person'], $request['number'], $request['image'], 
					$request['address'], $request['telephone'],
					$request['latitude'], $request['longitude'], $request['uid'], $date,
					$request['id']);
		$sql = update_travel_item_sql();
		$result = DBFunction::getInstance()->update($sql, $params);
	}
	else
	{
		$params = array($name, $legal_person, $number, $image, 
						$address, $telephone, '0', 
						$latitude, $longitude, 
						$uid, $date, $uid, $date);
		$sql = add_travel_item_sql();
		$id = DBFunction::getInstance()->insert($sql, $params);
	}
	
	if($id)
	{
		$params = array($id, $request['desc'], $request['star_level'], $request['min_price'], 
					'0', '0', '0', $request['notice'], $request['traffic']);

		$sql = add_hostel_sql();
		$result = DBFunction::getInstance()->insert($sql, $params);
	}
	
	return $id;
}

function update_hostel($request)
{
	$date = time();

	$params = array($request['name'], $request['legal_person'], $request['number'], $request['image'], 
					$request['address'], $request['telephone'],
					$request['latitude'], $request['longitude'], $request['uid'], $date,
					$request['id']);
	$sql = update_travel_item_sql();
	$result = DBFunction::getInstance()->update($sql, $params);
	
	if($result !== false)
	{
		$params = array($request['desc'], $request['star_level'], $request['min_price'], $request['notice'], 
						$request['traffic'], $request['id']);

		$sql = update_hostel_sql();
		$result = DBFunction::getInstance()->update($sql, $params);
	}
	
	return ($result !== false) ? true : false;
}

function delete_hostel($request)
{
	$sql = delete_hostel_sql();
	$result = DBFunction::getInstance()->execute($sql, array($request['id']));
	
	$sql = delete_travel_item_sql();
	$result = DBFunction::getInstance()->execute($sql, array($request['id']));
	
	return ($result !== false) ? true : false;
}

function get_hostel_room_list($request)
{
	$hostel_id = isset($request['hid']) ? $request['hid'] : '';
	
	$key = md5("get_hostel_room_list_$hostel_id");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$sql = get_hostel_room_list_sql();
	$results = DBFunction::getInstance()->getAll($sql, array($hostel_id));
	
	if(check_array($results))
	{
		cache_set($key, $results);
		return $results;
	}
	else
	{
		return array();
	}
}

function add_hostel_room($request)
{
	$params = array($request['hid'], $request['name'], $request['area'], $request['price'], $request['image']);
	
	$sql = add_hostel_room_sql();
	$result = DBFunction::getInstance()->insert($sql, $params);
	
	return $result;
}

function get_hostel_room($request)
{
	$sql = get_hostel_room_sql();
	$results = DBFunction::getInstance()->getRow($sql, array($request['id'], $request['hid']));
	
	return check_array($results) ? $results : array();
}

function update_hostel_room($request)
{
	$params = array($request['name'], $request['area'], $request['price'], $request['image'], $request['id'], $request['hid']);
	
	$sql = update_hostel_room_sql();
	$result = DBFunction::getInstance()->update($sql, $params);

	return ($result !== false) ? true : false;
}

function delete_hostel_room($request)
{
	$ids = isset($request['ids']) ? $request['ids'] : '';
	$hid = isset($request['hid']) ? $request['hid'] : '';
	
	if(stripos($ids, ','))
	{
		$ids = explode(',', $ids);
		
		foreach($ids as $id)
		{
			$params = array($id, $hid);

			$sql = delete_hostel_room_sql();
			$result = DBFunction::getInstance()->execute($sql, $params);
		}
	}
	else
	{
		$params = array($ids, $hid);
	
		$sql = delete_hostel_room_sql();
		$result = DBFunction::getInstance()->execute($sql, $params);
	}

	return ($result !== false) ? true : false;
}

function get_hostel_history_price($request)
{
	$id = isset($request['id']) ? $request['id'] : '';
	$name = isset($request['name']) ? $request['name'] : '';
	$is_gov = isset($request['is_gov']) ? $request['is_gov'] : '0';
			
	$limit = isset($request['limit']) ? $request['limit'] : '10';
	$offset = isset($request['offset']) ? $request['offset'] : '0';
	$offset *= $limit;
	
	$key = md5("get_hostel_history_price_$name"."_$id"."_$limit"."_$offset");
	
	$result = cache_get($key);
	if($result !== false)
	{
		return $result;
	}
	
	$params = array();
	$where = '1';
	if($id)
	{
		$params[] = $id;
		$where = 'ti.`id` = ?';
	}
	//else if(strlen($name) > 3)
	else if($name)
	{
		$params[] = '%'.strtolower($name).'%';
		$where = 'ti.`name` like ?';
	}
	
	$sql =  'select count(distinct ti.`id`), ';
	$sql .= get_hostel_history_price_sql();
	$sql .= $where;
	
	$params[] = $is_gov;
	$sql .= ' and `is_gov`=?';
	$sql .= ' group by ti.`id`';
	$sql .= ' order by hph.`create_date` desc, hph.item_id asc';
	
	$result['count'] = DBFunction::getInstance()->getRowCount($sql, $params);
	$result['result'] = array();
	
	if($result['count'] > 0)
	{
		$result['result'] = DBFunction::getInstance()->selectLimit($sql, $limit, $offset, $params);
		
		cache_set($key, $result);
	}
	
	return $result;
}

function get_hostel_room_history_price($request)
{
	$id = isset($request['id']) ? $request['id'] : '';
	$from = isset($request['from']) ? strtotime($request['from']) : '';
	$to = isset($request['to']) ? strtotime($request['to']) : '';
	$is_gov = isset($request['is_gov']) ? $request['is_gov'] : '0';
	
	$price1 = isset($request['price1']) ? $request['price1'] : '';
	$price2 = isset($request['price2']) ? $request['price2'] : '';
		
	$limit = isset($request['limit']) ? $request['limit'] : '10';
	$offset = isset($request['offset']) ? $request['offset'] : '0';
	$offset *= $limit;
	
	$key = md5("get_hostel_history_price__$id"."_$from"."_$to"."_$limit"."_$offset");
	
	$result = cache_get($key);
	if($result !== false)
	{
		return $result;
	}
	
	$params = array();
	$where = '1';
	if($id)
	{
		$params[] = $id;
		$where = 'ti.`id` = ?';
	}
	
	if($from)
	{
		$params[] = $from;
		if($where == '1')
		{
			$where = 'hph.`create_date` >= ?';
		}
		else
		{
			$where .= ' and hph.`create_date` >= ?';
		}
	}
	if($to)
	{
		$params[] = $to;
		if($where == '1')
		{
			$where = 'hph.`create_date` <= ?';
		}
		else
		{
			$where .= ' and hph.`create_date` <= ?';
		}
	}
	
	if($price1)
	{
		$params[] = $price1;
		if($where == '1')
		{
			$where = 'hph.`price` >= ?';
		}
		else
		{
			$where .= ' and hph.`price` >= ?';
		}
	}
	if($price2)
	{
		$params[] = $price2;
		if($where == '1')
		{
			$where = 'hph.`price` <= ?';
		}
		else
		{
			$where .= ' and hph.`price` <= ?';
		}
	}
	
	$sql = 'select ';
	$sql .= get_hostel_history_price_sql();
	$sql .= $where;
	
	$params[] = $is_gov;
	$sql .= ' and `is_gov`=?';
	$sql .= ' order by hph.`create_date` desc, hph.item_id asc';
	
	$result['count'] = DBFunction::getInstance()->getRowCount($sql, $params);
	$result['result'] = array();
	
	if($result['count'] > 0)
	{
		$result['result'] = DBFunction::getInstance()->selectLimit($sql, $limit, $offset, $params);
		
		cache_set($key, $result);
	}
	
	return $result;
}

function add_hostel_history_price($request)
{
	$params = array($request['id'], $request['room_name'], $request['price'], $request['range'],
					$request['is_gov'], $request['create_date']);
					
	$sql = add_hostel_history_price_sql();
	$result = DBFunction::getInstance()->insert($sql, $params);
	
	return $result;
}

function delete_hostel_history_price($id)
{
	$sql = delete_hostel_history_price_sql();
	$result = DBFunction::getInstance()->execute($sql, array($id));
	
	return ($result !== false) ? true : false;
}

//TODO, when name == param, show first
function get_scenic_list($request)
{
	$ids = isset($request['ids']) ? $request['ids'] : '';
	$name = isset($request['name']) ? $request['name'] : '';
	
	$latitude1 = isset($request['latitude1']) ? $request['latitude1'] : '';
	$latitude2 = isset($request['latitude2']) ? $request['latitude2'] : '';
	$longitude1 = isset($request['longitude1']) ? $request['longitude1'] : '';
	$longitude2 = isset($request['longitude2']) ? $request['longitude2'] : '';
	
	$sort_fields = array('name'=>'ti.`name`', 'price'=>'ts.price_score', 'service'=>'ts.service_score', 'env'=>'ts.env_score');
	
	$sort = (isset($request['sort']) && isset($sort_fields[$request['sort']])) ? $sort_fields[$request['sort']] : 'ti.`name`';
	$order = (isset($request['order']) && $request['order'] == 'desc') ? 'desc' : 'asc';
	
	$limit = isset($request['limit']) ? $request['limit'] : DEFAULT_PER_PAGE;
	$offset = isset($request['offset']) ? $request['offset'] : '0';
	$offset *= $limit;
	
	$key = md5("get_scenic_list_$name"."_$ids"."_$latitude1"."_$longitude1"."_$latitude2"."_$longitude2"."_$sort"."_$order"."_$limit"."_$offset");
	
	$result = cache_get($key);
	if($result !== false)
	{
		return $result;
	}
	
	$params = array();
	$where = '1';
	if($longitude1 && $longitude2 && $latitude1 && $latitude2)
	{
		$params = array($longitude2, $longitude1, $latitude2, $latitude1);
		$where = 'ti.`longitude` <= ?  and ti.`longitude` >= ? and ti.`latitude` <= ? and ti.`latitude` >= ?';
	}
	else if($name)
	{
		$params = array('%'.strtolower($name).'%');
		$where = 'ti.`name` like ?';
	}
	else if($ids)
	{
		$where = "ti.`id` in ($ids)";	
	}
	
	
	$sql = get_scenic_list_sql();
	$sql .= $where;
	$sql .= " order by $sort $order";
	
	$result['count'] = DBFunction::getInstance()->getRowCount($sql, $params);
	$result['result'] = array();
	
	if($result['count'] > 0)
	{
		$result['result'] = DBFunction::getInstance()->selectLimit($sql, $limit, $offset, $params);
		
		cache_set($key, $result);
	}
	
	return $result;
}

function get_scenic_history_price($request)
{
	$id = isset($request['id']) ? $request['id'] : '';
	$name = isset($request['name']) ? $request['name'] : '';
	$from = isset($request['from']) ? strtotime($request['from']) : '';
	$to = isset($request['to']) ? strtotime($request['to']) : '';
	$is_gov = isset($request['is_gov']) ? $request['is_gov'] : '0';
		
	$limit = isset($request['limit']) ? $request['limit'] : DEFAULT_PER_PAGE;
	$offset = isset($request['offset']) ? $request['offset'] : '0';
	$offset *= $limit;
	
	$key = md5("get_scenic_history_price_$name"."_$from"."_$to"."_$limit"."_$offset");
	
	$result = cache_get($key);
	if($result !== false)
	{
		return $result;
	}
	
	$params = array();
	$where = '1';
	
	if($id)
	{
		$params[] = $id;
		$where = 'ti.`id` = ?';
	}
	//if(strlen($name) > 3)
	if($name)
	{
		$params[] = '%'.strtolower($name).'%';
		$where = 'ti.`name` like ?';
	}
	if($from)
	{
		$params[] = $from;
		if($where == '1')
		{
			$where = 'hps.`create_date` >= ?';
		}
		else
		{
			$where .= ' and hps.`create_date` >= ?';
		}
	}
	if($to)
	{
		$params[] = $to;
		if($where == '1')
		{
			$where = 'hps.`create_date` <= ?';
		}
		else
		{
			$where .= ' and hps.`create_date` <= ?';
		}
	}
	
	$sql = get_scenic_history_price_sql();
	$sql .= $where;
	
	$params[] = $is_gov;
	$sql .= ' and `is_gov`=?';
	$sql .= ' order by ts.favorite_count desc';
	
	$result['count'] = DBFunction::getInstance()->getRowCount($sql, $params);
	$result['result'] = array();
	
	if($result['count'] > 0)
	{
		$result['result'] = DBFunction::getInstance()->selectLimit($sql, $limit, $offset, $params);
		
		cache_set($key, $result);
	}
	
	return $result;
}

function add_scenic_history_price($request)
{
	$params = array($request['id'], $request['min_price'], '0', 
					$request['is_gov'], $request['create_date']);
					
	$sql = add_scenic_history_price_sql();
	$result = DBFunction::getInstance()->insert($sql, $params);
	
	return $result;
}

function delete_scenic_history_price($id)
{
	$sql = delete_scenic_history_price_sql();
	$result = DBFunction::getInstance()->execute($sql, array($id));
	
	return ($result !== false) ? true : false;
}

function get_scenic_info($request)
{
	$id = isset($request['sid']) ? $request['sid'] : '';
	
	$key = md5("get_scenic_info_$id");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$sql = get_avg_comment_score_sql();
	$temp_result = DBFunction::getInstance()->getRow($sql, array($id));
	
	// update custome comment score
	if(check_array($temp_result))
	{
		list($service_score, $env_score, $price_score) = array_values($temp_result);
		//$total = round(($service_score + $env_score + $price_score) / 3, 2);
		$total = $service_score;
		
		$sql = update_total_score_sql();
		DBFunction::getInstance()->update($sql, array($total, $id));
		
		$sql = update_scenic_score_sql();
		DBFunction::getInstance()->update($sql, array($service_score, $id));
	} 
	
	$sql = get_scenic_info_sql();
	$results = DBFunction::getInstance()->getRow($sql, array($id));
	
	if(check_array($results))
	{
		$sql = get_image_list_sql();
		$results['image_list'] = DBFunction::getInstance()->getAll($sql, array($id));
		
		//$sql = get_scenic_traffic_list_sql();
		//$results['traffic_list'] = DBFunction::getInstance()->getAll($sql, array($id));
		
		cache_set($key, $results);
		return $results;
	}
	else
	{
		return array();
	}
}

function add_scenic($request)
{
	$date = time();
	
	$uid = isset($request['uid']) ? $request['uid'] : '';
	
	$name = isset($request['name']) ? $request['name'] : '';
	$legal_person = isset($request['legal_person']) ? $request['legal_person'] : '';
	$number = isset($request['number']) ? $request['number'] : '';
	$image = isset($request['image']) ? $request['image'] : '';
	
	$telephone = isset($request['telephone']) ? $request['telephone'] : '';
	$address = isset($request['address']) ? $request['address'] : '';
	
	$longitude = isset($request['longitude']) ? (float)$request['longitude'] : '';
	$latitude = isset($request['latitude']) ? (float)$request['latitude'] : '';
	$create_date = isset($request['create_date']) ? $request['create_date'] : '';

	$params = array($name, $legal_person, $number, $image, 
					$address, $telephone, '0', 
					$longitude, $latitude, 
					$uid, $create_date, $uid, $date);
	$sql = add_travel_item_sql();
	$result = DBFunction::getInstance()->insert($sql, $params);
	
	if($result)
	{
		$params = array($result, $request['desc'], $request['notice'], $request['traffic'],
					$request['min_price'], '0', '0', $request['is_hot']);

		$sql = add_scenic_sql();
		$result = DBFunction::getInstance()->insert($sql, $params);
		
		$request['id'] = $result;
		$request['is_gov'] = '0';
		add_scenic_history_price($request);
	}
	
	return $result;
}

function update_scenic($request)
{
	$date = time();
	
	$params = array($request['name'], $request['legal_person'], $request['number'], $request['image'], 
					$request['address'], $request['telephone'],
					$request['latitude'], $request['longitude'], $request['uid'], $date,
					$request['id']);
	$sql = update_travel_item_sql();
	$result = DBFunction::getInstance()->update($sql, $params);
	
	if($result !== false)
	{
		$params = array($request['desc'], $request['notice'], $request['traffic'], $request['min_price'],
				$request['is_hot'], $request['id']);
	
		$sql = update_scenic_sql();
		$result = DBFunction::getInstance()->update($sql, $params);
		
		$request['is_gov'] = '0';
		add_scenic_history_price($request);
	}

	return ($result !== false) ? true : false;
}

function delete_scenic($id)
{
	$sql = delete_scenic_sql();
	$result = DBFunction::getInstance()->execute($sql, array($id));
	
	$sql = delete_travel_item_sql();
	$result = DBFunction::getInstance()->execute($sql, array($id));
	
	return ($result !== false) ? true : false;
}

function get_shop_list($request)
{
	$ids = isset($request['ids']) ? $request['ids'] : '';
	$name = isset($request['name']) ? $request['name'] : '';
	
	$latitude1 = isset($request['latitude1']) ? $request['latitude1'] : '';
	$latitude2 = isset($request['latitude2']) ? $request['latitude2'] : '';
	$longitude1 = isset($request['longitude1']) ? $request['longitude1'] : '';
	$longitude2 = isset($request['longitude2']) ? $request['longitude2'] : '';
	
	$sort_fields = array('name'=>'ti.`name`', 'price'=>'ts.price_score', 'service'=>'ts.service_score', 'env'=>'ts.env_score');
	
	$sort = (isset($request['sort']) && isset($sort_fields[$request['sort']])) ? $sort_fields[$request['sort']] : 'ti.`name`';
	$order = (isset($request['order']) && $request['order'] == 'desc') ? 'desc' : 'asc';
	
	$limit = isset($request['limit']) ? $request['limit'] : DEFAULT_PER_PAGE;
	$offset = isset($request['offset']) ? $request['offset'] : '0';
	$offset *= $limit;
	
	$key = md5("get_shop_list_$name"."_$latitude1"."_$longitude1"."_$latitude2"."_$longitude2"."_$ids"."_$sort"."_$order"."_$limit"."_$offset");
	
	$result = cache_get($key);
	if($result !== false)
	{
		return $result;
	}
	
	$params = array();
	$where = '1';
	if($longitude1 && $longitude2 && $latitude1 && $latitude2)
	{
		$params = array($longitude2, $longitude1, $latitude2, $latitude1);
		$where = 'ti.`longitude` <= ?  and ti.`longitude` >= ? and ti.`latitude` <= ? and ti.`latitude` >= ?';
	}
	else if($name)
	{
		$params = array('%'.strtolower($name).'%');
		$where = 'ti.`name` like ?';
	}
	else if($ids)
	{
		$where = "ti.`id` in ($ids)";
	}
	
	$sql = get_shop_list_sql();
	$sql .= $where;
	$sql .= " order by $sort $order";
	
	$result['count'] = DBFunction::getInstance()->getRowCount($sql, $params);
	$result['result'] = array();
	
	if($result['count'] > 0)
	{
		$result['result'] = DBFunction::getInstance()->selectLimit($sql, $limit, $offset, $params);
		
		cache_set($key, $result);
	}
	
	return $result;
}

function add_shop($request)
{
	$date = time();

	$uid = isset($request['uid']) ? $request['uid'] : '';
	
	$name = isset($request['name']) ? $request['name'] : '';
	$legal_person = isset($request['legal_person']) ? $request['legal_person'] : '';
	$number = isset($request['number']) ? $request['number'] : '';
	$image = isset($request['image']) ? $request['image'] : '';
	
	$telephone = isset($request['telephone']) ? $request['telephone'] : '';
	$address = isset($request['address']) ? $request['address'] : '';
	
	$longitude = isset($request['longitude']) ? (float)$request['longitude'] : '';
	$latitude = isset($request['latitude']) ? (float)$request['latitude'] : '';

	$params = array($name, $legal_person, $number, $image, 
					$address, $telephone, '0', 
					$longitude, $latitude, 
					$uid, $date, $uid, $date);
	$sql = add_travel_item_sql();
	$result = DBFunction::getInstance()->insert($sql, $params);
	
	if($result)
	{
		$params = array($result, $request['desc'], '0', '0', '0');

		$sql = add_shop_sql();
		$result = DBFunction::getInstance()->insert($sql, $params);
	}
	
	return $result;
}

function get_shop_info($request)
{
	$id = isset($request['sid']) ? $request['sid'] : '';
	
	$key = md5("get_shop_info_$id");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$sql = get_avg_comment_score_sql();
	$temp_result = DBFunction::getInstance()->getRow($sql, array($id));
	
	// update custome comment score
	if(check_array($temp_result))
	{		
		list($service_score, $env_score, $price_score) = array_values($temp_result);
		$total = round(($service_score + $env_score + $price_score) / 3, 2);
		
		$sql = update_total_score_sql();
		DBFunction::getInstance()->update($sql, array($total, $id));
		
		$sql = update_shop_score_sql();
		DBFunction::getInstance()->update($sql, array($service_score, $env_score, $price_score, $id));
	}
	
	$sql = get_shop_info_sql();
	$results = DBFunction::getInstance()->getRow($sql, array($id));
	
	if(check_array($results))
	{
		cache_set($key, $results);
		return $results;
	}
	else
	{
		return array();
	}
}

function update_shop($request)
{
	$date = time();
	
	$params = array($request['name'], $request['legal_person'], $request['number'], $request['image'], 
					$request['address'], $request['telephone'],
					$request['latitude'], $request['longitude'], $request['uid'], $date,
					$request['id']);
	$sql = update_travel_item_sql();
	$result = DBFunction::getInstance()->update($sql, $params);
	
	if($result !== false)
	{
		$params = array($request['desc'], $request['id']);
	
		$sql = update_shop_sql();
		$result = DBFunction::getInstance()->update($sql, $params);
	}

	return ($result !== false) ? true : false;
}

function delete_shop($id)
{
	$sql = delete_shop_sql();
	$result = DBFunction::getInstance()->execute($sql, array($id));
	
	$sql = delete_travel_item_sql();
	$result = DBFunction::getInstance()->execute($sql, array($id));
	
	return ($result !== false) ? true : false;
}

function get_shop_item_list($request)
{
	$shop_id = isset($request['sid']) ? $request['sid'] : '';
	
	$key = md5("get_shop_item_list_$shop_id");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$sql = get_shop_item_list_sql();
	$results = DBFunction::getInstance()->getAll($sql, array($shop_id));
	
	if(check_array($results))
	{
		cache_set($key, $results);
		return $results;
	}
	else
	{
		return array();
	}
}

function get_shop_item($request)
{
	$sql = get_shop_item_sql();
	$results = DBFunction::getInstance()->getRow($sql, array($request['id']));
	
	return check_array($results) ? $results : array();
}

function add_shop_item($request)
{
	$params = array($request['sid'], $request['name'], $request['image'],
					$request['product_place'], $request['desc'],
					$request['market_price'], $request['duty_free_price'],);
	
	$sql = add_shop_item_sql();
	$result = DBFunction::getInstance()->insert($sql, $params);
	
	return $result;
}

function update_shop_item($request)
{	
	$params = array($request['name'], $request['image'], $request['product_place'], $request['desc'],
				$request['market_price'], $request['duty_free_price'], 
				$request['id'], $request['sid']);
	
	$sql = update_shop_item_sql();
	$result = DBFunction::getInstance()->update($sql, $params);

	return ($result !== false) ? true : false;
}

function delete_shop_item($request)
{
	$ids = isset($request['ids']) ? $request['ids'] : '';
	$sid = isset($request['sid']) ? $request['sid'] : '';
	
	$result = '';
	if(stripos($ids, ','))
	{
		$ids = explode(',', $ids);
		
		foreach($ids as $id)
		{
			$params = array($id, $sid);

			$sql = delete_shop_item_sql();
			$result = DBFunction::getInstance()->execute($sql, $params);
		}
	}
	else
	{
		$params = array($ids, $sid);
	
		$sql = delete_shop_item_sql();
		$result = DBFunction::getInstance()->execute($sql, $params);
	}

	return ($result !== false) ? true : false;
}

function get_credit_list($request)
{
	$name = isset($request['name']) ? trim($request['name']) : '';
	$type = isset($request['type']) ? $request['type'] : '';
	$is_auth = isset($request['is_auth']) ? $request['is_auth'] : '';
	
	$limit = isset($request['limit']) ? $request['limit'] : DEFAULT_PER_PAGE;
	$offset = isset($request['offset']) ? $request['offset'] : '0';
	$offset *= $limit;
	
	$order = (isset($request['order']) && $request['order'] == 'asc') ? 'asc' : 'desc';
	
	$table_list = array('bu'=>'tra_travel_business', 'ho'=>'tra_travel_hostel', 'sc'=>'tra_travel_scenic', 'sh'=>'tra_travel_shop');
	$table = isset($table_list[$type]) ? $table_list[$type] : '';
	
	$key = md5("get_credit_list_$type"."_$limit"."_$offset");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$params = array();
	$where = '1';
	if($name)
	{
		$params[] = '%'.$name.'%';
		$params[] = '%'.$name.'%';
		$where = 'ti.`name` like ? or ti.`legal_person` like ?';
	}
	if($is_auth)
	{
		$params[] = ($is_auth == 'true') ? '1' : '0';
		$where = ($where == '1') ? 'ti.`is_auth`=?' : ' and ti.`is_auth`=?';
	}
	
	$sql = get_credit_list_sql();
	if($table)
	{
		$sql .= " inner join $table f on ti.id=f.item_id";
	}
	$sql .= " where $where";
	$sql .= " order by ti.customer_score $order";

	$result['count'] = DBFunction::getInstance()->getRowCount($sql, $params);
	$result['result'] = array();
	
	if($result['count'] > 0)
	{
		$result['result'] = DBFunction::getInstance()->selectLimit($sql, $limit, $offset, $params);
		
		cache_set($key, $result);
	}
	
	return $result;
}

function set_auth_status($request)
{
	$status = $request['status'] ? '1' : '0';
	
	$sql = set_auth_status_sql();
	$result = DBFunction::getInstance()->update($sql, array($status, $request['id']));
	
	return ($result !== false) ? true : false;
}

function get_complaint_list($request)
{
	$name = isset($request['name']) ? $request['name'] : '';
	$is_show = isset($request['is_show']) ? $request['is_show'] : '';
	
	$limit = isset($request['limit']) ? $request['limit'] : DEFAULT_PER_PAGE;
	$offset = isset($request['offset']) ? $request['offset'] : '0';
	$offset *= $limit;
		
	$key = md5("get_complaint_list_$name"."_$is_show"."_$limit"."_$offset");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$sql = get_complaint_sql();
	$params = array();
	if($name)
	{
		$name = '%'.$name.'%';
		$params = array($name, $name);
		$sql .= 'b.`name` like ? or b.`legal_person` like ?';
	}
	else if($is_show)
	{
		$params = array($is_show);
		$sql .= 'cc.`is_show` = ?';
	}
	else
	{
		$sql .= '1';
	}
	
	$results = DBFunction::getInstance()->selectLimit($sql, $limit, $offset, $params);
	
	if(check_array($results))
	{
		cache_set($key, $results);
		return $results;
	}
	else
	{
		return array();
	}
}

function get_complaint($request)
{
	$cid = isset($request['cid']) ? $request['cid'] : '';
	
	$key = md5("get_complaint_$cid");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$sql = get_complaint_sql();
	$sql .= 'cc.`id`=?';
	
	$results = DBFunction::getInstance()->getRow($sql, array($cid));
	
	if(check_array($results))
	{
		cache_set($key, $results);
		return $results;
	}
	else
	{
		return array();
	}
}

function get_complaint_deal_list($request)
{
	$cid = isset($request['cid']) ? $request['cid'] : '';
		
	$key = md5("get_complaint_deal_list_$cid");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$sql = get_complaint_deal_list_sql();
	
	$results = DBFunction::getInstance()->getAll($sql, array($cid));
	
	if(check_array($results))
	{
		cache_set($key, $results);
		return $results;
	}
	else
	{
		return array();
	}
}

function get_seafood_units_list()
{
	$sql = get_seafood_units_list_sql();
	
	$results = DBFunction::getInstance()->getAll($sql);
	
	return $results;
}

function add_seafood_units($request)
{	
	$sql = add_seafood_units_sql();
	$result = DBFunction::getInstance()->insert($sql, array($request['units']));
	
	return $result;
}

function delete_seafood_units($id)
{
	$sql = delete_seafood_units_sql();
	$result = DBFunction::getInstance()->execute($sql, array($id));
	
	return ($result !== false) ? true : false;
}

function get_room_types()
{
	$sql = get_room_types_sql();
	
	$results = DBFunction::getInstance()->getAll($sql);
	
	return $results;
}

function add_room_type($request)
{	
	$sql = add_room_type_sql();
	$result = DBFunction::getInstance()->insert($sql, array($request['name']));
	
	return $result;
}

function delete_room_type($id)
{
	$sql = delete_room_type_sql();
	$result = DBFunction::getInstance()->execute($sql, array($id));
	
	return ($result !== false) ? true : false;
}

function add_travel_item_image($id, $image)
{
	$sql = add_travel_item_image_sql();
	$result = DBFunction::getInstance()->insert($sql, array($id, $image));
	
	return $result;
}

function delete_travel_item_image($id)
{
	$sql = delete_travel_item_image_sql();
	$result = DBFunction::getInstance()->execute($sql, array($id));
	
	return ($result !== false) ? true : false;
}

function get_news_list($request)
{
	$keyword = isset($request['keyword']) ? $request['keyword'] : '';
	
	$limit = isset($request['limit']) ? $request['limit'] : DEFAULT_PER_PAGE;
	$offset = isset($request['offset']) ? $request['offset'] : '0';
	$offset *= $limit;
	
	$key = md5("get_news_list_$keyword"."_$limit"."_$offset");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$where = '1';
	$params = array();
	if($keyword)
	{
		$keyword = '%'.strtolower($keyword).'%';
		$params = array($keyword, $keyword);
		$where = '`title` like ? or `desc` like ?';
	}
	
	$sql = get_news_sql();
	$sql .= $where;
	$sql .= " order by publish_date desc";
	
	$result['count'] = DBFunction::getInstance()->getRowCount($sql, $params);
	$result['result'] = array();
	
	if($result['count'] > 0)
	{
		$result['result'] = DBFunction::getInstance()->selectLimit($sql, $limit, $offset, $params);
		
		cache_set($key, $result);
	}
	
	return $result;
}

function get_news_info($request)
{
	$id = isset($request['id']) ? $request['id'] : '';
	
	$key = md5("get_news_info_$id");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$sql = get_news_sql();
	$sql .= '`id`=?';
	$results = DBFunction::getInstance()->getRow($sql, array($id));
	
	if(check_array($results))
	{
		cache_set($key, $results);
		return $results;
	}
	else
	{
		return array();
	}
}

function add_news($request)
{
	$params = array($request['type'], $request['title'], $request['desc'], $request['author'], 
					$request['publish_date'],$request['is_show']);
	
	$sql = add_news_sql();
	$result = DBFunction::getInstance()->insert($sql, $params);
	
	return $result;
}

function update_news($request)
{	
	$params = array($request['type'], $request['title'], $request['desc'], $request['author'], 
					$request['publish_date'],$request['is_show'], $request['id']);
	
	$sql = update_news_sql();
	$result = DBFunction::getInstance()->update($sql, $params);

	return ($result !== false) ? true : false;
}

function delete_news($id)
{
	$sql = delete_news_sql();
	$result = DBFunction::getInstance()->execute($sql, array($id));

	return ($result !== false) ? true : false;
}

function get_travel_item_list($request)
{
	$name = isset($request['name']) ? '%'.strtolower($request['name']).'%' : '';
	
	$limit = isset($request['limit']) ? $request['limit'] : DEFAULT_PER_PAGE;
	$offset = isset($request['offset']) ? $request['offset'] : '0';
	$offset *= $limit;
	
	$key = md5("get_travel_item_list_$name"."_$limit"."_$offset");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$sql = get_travel_item_list_sql();
	$results = DBFunction::getInstance()->selectLimit($sql, $limit, $offset, array($name, $name));
	
	if(check_array($results))
	{
		cache_set($key, $results);
		return $results;
	}
	else
	{
		return array();
	}
}

function get_travel_item($request)
{
	$id = isset($request['id']) ? $request['id'] : '';
	$name = isset($request['name']) ? $request['name'] : '';
	
	$key = md5("get_travel_item_$name"."_$id");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$sql = get_travel_item_sql();
	$results = DBFunction::getInstance()->getRow($sql, array($id, $name));
	
	if(check_array($results))
	{
		cache_set($key, $results);
		return $results;
	}
	else
	{
		return array();
	}
}

function update_shop_item_up_down_count($request)
{
	if($request['type'] == 'up')
	{
		$sql = update_shop_item_up_count_sql();
	}
	else
	{
		$sql = update_shop_item_down_count_sql();
	}
	
	$result = DBFunction::getInstance()->update($sql, array($request['id']));
	
	add_up_down_record(array('uid'=>$request['uid'], 'item_id'=>$request['id'], 'type'=>'shop_item'));
	
	return ($result !== false) ? true : false;
}

function add_up_down_record($request)
{
	$date = time();
	delete_up_down_record();
	
	$sql = add_up_down_record_sql();
	$result = DBFunction::getInstance()->insert($sql, array($request['uid'], $request['item_id'], $request['type'], $date));
	
	return $result;
}

function check_up_down_records_exists($request)
{
	$sql = check_up_down_records_exists_sql();
	$result = DBFunction::getInstance()->getOne($sql, array($request['uid'], $request['item_id'], $request['type']));
	
	return $result ? true : false;
}

function delete_up_down_record()
{
	$date = time() - 24*3600;
	
	$sql = delete_up_down_record_sql();
	$result = DBFunction::getInstance()->getOne($sql, array($date));
	
	return ($result !== false) ? true : false;
}

function add_feedback($request)
{
	$date = time();
	
	$sql = add_feedback_sql();
	$result = DBFunction::getInstance()->insert($sql, array($request['feedback'], $request['name'], $request['telephone'], $request['email'], $date));
	
	return $result;
}

/*
function get_food_culture()
{
	$sql = get_food_culture_sql();
	$result = DBFunction::getInstance()->getAll($sql);
	
	return $result;
}
*/

function get_complaint_comment_list($request)
{
	$limit = isset($request['limit']) ? $request['limit'] : DEFAULT_PER_PAGE;
	$offset = isset($request['offset']) ? $request['offset'] : '0';
	$offset *= $limit;
	
	$key = md5("get_complaint_comment_list"."_$limit"."_$offset");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$sql = get_complaint_comment_sql();
	
	$params = array($request['cid']);
	
	$result['count'] = DBFunction::getInstance()->getRowCount($sql, $params);
	$result['result'] = array();
	
	if($result['count'] > 0)
	{
		$result['result'] = DBFunction::getInstance()->selectLimit($sql, $limit, $offset, $params);
		
		cache_set($key, $result);
	}
	
	return $result;
}

function add_complaint_comment($request)
{
	$date = time();
	
	$sql = add_complaint_comment_sql();
	$result = DBFunction::getInstance()->insert($sql, array($request['cid'], $request['uid'], $request['desc'], $date));
	
	return $result;
}

function delete_complaint_comment($request)
{
	$sql = delete_complaint_comment_sql();
	$result = DBFunction::getInstance()->execute($sql, array($request['id']));
	
	return ($result !== false) ? true : false;
}

function get_shop_item_comment_list($request)
{
	$limit = isset($request['limit']) ? $request['limit'] : DEFAULT_PER_PAGE;
	$offset = isset($request['offset']) ? $request['offset'] : '0';
	$offset *= $limit;
	
	$key = md5("get_shop_item_comment_list"."_$limit"."_$offset");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$sql = get_shop_item_comment_sql();
	$params = array($request['id']);
	
	$result['count'] = DBFunction::getInstance()->getRowCount($sql, $params);
	$result['result'] = array();
	
	if($result['count'] > 0)
	{
		$result['result'] = DBFunction::getInstance()->selectLimit($sql, $limit, $offset, $params);
		
		cache_set($key, $result);
	}
	
	return $result;
}

function add_shop_item_comment($request)
{
	$date = time();
	
	$sql = add_shop_item_comment_sql();
	$result = DBFunction::getInstance()->insert($sql, array($request['id'], $request['uid'], $request['desc'], $date));
	
	return $result;
}

function delete_shop_item_comment($request)
{
	$sql = delete_shop_item_comment_sql();
	$result = DBFunction::getInstance()->execute($sql, array($request['id']));
	
	return ($result !== false) ? true : false;
}

/*
function get_travel_tips()
{	
	$key = md5("get_travel_tips");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$sql = get_travel_tips_sql();
	$results = DBFunction::getInstance()->getAll($sql);
	
	if(check_array($results))
	{
		cache_set($key, $results);
		return $results;
	}
	else
	{
		return array();
	}
}
*/

function get_tips_list($request)
{
	$keyword = isset($request['keyword']) ? $request['keyword'] : '';
	
	$limit = isset($request['limit']) ? $request['limit'] : DEFAULT_PER_PAGE;
	$offset = isset($request['offset']) ? $request['offset'] : '0';
	$offset *= $limit;
	
	$key = md5("get_tips_list_$keyword"."_$limit"."_$offset");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$where = '1';
	$params = array();
	if($keyword)
	{
		$keyword = '%'.strtolower($keyword).'%';
		$params = array($keyword, $keyword);
		$where = '`title` like ? or `desc` like ?';
	}
	
	$sql = get_tips_sql();
	$sql .= $where;
	$sql .= " order by create_date desc";
	
	$result['count'] = DBFunction::getInstance()->getRowCount($sql, $params);
	$result['result'] = array();
	
	if($result['count'] > 0)
	{
		$result['result'] = DBFunction::getInstance()->selectLimit($sql, $limit, $offset, $params);
		
		cache_set($key, $result);
	}
	
	return $result;
}

function get_tips_info($request)
{
	$id = isset($request['id']) ? $request['id'] : '';
	
	$key = md5("get_tips_info_$id");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$sql = get_tips_sql();
	$sql .= '`id`=?';
	$results = DBFunction::getInstance()->getRow($sql, array($id));
	
	if(check_array($results))
	{
		cache_set($key, $results);
		return $results;
	}
	else
	{
		return array();
	}
}

function add_tips($request)
{
	$params = array($request['title'], $request['desc'], $request['create_date']);
	
	$sql = add_tips_sql();
	$result = DBFunction::getInstance()->insert($sql, $params);
	
	return $result;
}

function update_tips($request)
{	
	$params = array($request['title'], $request['desc'], $request['create_date'], $request['id']);
	
	$sql = update_tips_sql();
	$result = DBFunction::getInstance()->update($sql, $params);

	return ($result !== false) ? true : false;
}

function delete_tips($id)
{
	$sql = delete_tips_sql();
	$result = DBFunction::getInstance()->execute($sql, array($id));

	return ($result !== false) ? true : false;
}

function get_food_culture_list($request)
{
	$keyword = isset($request['keyword']) ? $request['keyword'] : '';
	
	$limit = isset($request['limit']) ? $request['limit'] : DEFAULT_PER_PAGE;
	$offset = isset($request['offset']) ? $request['offset'] : '0';
	$offset *= $limit;
	
	$key = md5("get_food_culture_list_$keyword"."_$limit"."_$offset");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$where = '1';
	$params = array();
	if($keyword)
	{
		$keyword = '%'.strtolower($keyword).'%';
		$params = array($keyword);
		$where = '`desc` like ?';
	}
	
	$sql = get_food_culture_sql();
	$sql .= $where;
	$sql .= " order by `create_date` desc";
	
	$result['count'] = DBFunction::getInstance()->getRowCount($sql, $params);
	$result['result'] = array();
	
	if($result['count'] > 0)
	{
		$result['result'] = DBFunction::getInstance()->selectLimit($sql, $limit, $offset, $params);
		
		cache_set($key, $result);
	}
	
	return $result;
}

function get_food_culture_info($request)
{
	$id = isset($request['id']) ? $request['id'] : '';
	
	$key = md5("get_food_culture_info_$id");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$sql = get_food_culture_sql();
	$sql .= '`id`=?';
	$results = DBFunction::getInstance()->getRow($sql, array($id));
	
	if(check_array($results))
	{
		cache_set($key, $results);
		return $results;
	}
	else
	{
		return array();
	}
}

function add_food_culture($request)
{
	$params = array($request['title'], $request['desc'], $request['create_date']);
	
	$sql = add_food_culture_sql();
	$result = DBFunction::getInstance()->insert($sql, $params);
	
	return $result;
}

function update_food_culture($request)
{	
	$params = array($request['title'], $request['desc'], $request['create_date'], $request['id']);
	
	$sql = update_food_culture_sql();
	$result = DBFunction::getInstance()->update($sql, $params);

	return ($result !== false) ? true : false;
}

function delete_food_culture($id)
{
	$sql = delete_food_culture_sql();
	$result = DBFunction::getInstance()->execute($sql, array($id));

	return ($result !== false) ? true : false;
}

function get_business_user_list($request)
{
	$name = isset($request['name']) ? $request['name'] : '';
	
	$limit = isset($request['limit']) ? $request['limit'] : '20';
	$offset = isset($request['offset']) ? $request['offset'] : '0';
	$offset *= $limit;
	
	$key = md5("get_business_user_list_$name"."_$limit"."_$offset");
	
	$result = cache_get($key);
	if($result !== false)
	{
		return $result;
	}
	
	$params = array();
	$where = '1';
	if($name)
	{
		$name = '%'.strtolower($request['name']).'%';
		$params = array($name, $name, $name);
		$where = 'u.`name` like ?
	or ti.`name` like ?
	or ti.`legal_person` like ?';
	}
	
	$sql = get_business_user_list_sql();
	$sql .= $where;
	$sql .= " order by u.`name`,ti.`name` ";
	
	$result['count'] = DBFunction::getInstance()->getRowCount($sql, $params);
	$result['result'] = array();
	
	if($result['count'] > 0)
	{
		$result['result'] = DBFunction::getInstance()->selectLimit($sql, $limit, $offset, $params);
		
		cache_set($key, $result);
	}
	
	return $result;
}